package main

import (
	"fmt"
	"time"

	"github.com/go-redis/redis"
)

var rdb *redis.Client

func initClient() error {
	rdb = redis.NewClient(&redis.Options{
		Addr:        "47.101.61.17:6379",
		Password:    "redis123456",
		DB:          0,
		DialTimeout: 10 * time.Second,
	})
	//心跳
	_, err := rdb.Ping().Result()
	if err != nil {
		return err
	}
	return nil
}

func redisExample() {
	//kv读写
	err := rdb.Set("score", 100, 0).Err()
	if err != nil {
		fmt.Printf("set score failed,err:%v\n", err)
		return
	}
	fmt.Println("set success")
}

func main() {
	err := initClient()
	if err != nil {
		fmt.Println("redis connect error:", err)
		return
	}
	redisExample()

}
